define(function(require, exports, module) {
    
    var rk = require('rk');
    
    // require('commonjs');
    
    
    
    
    

    var currentPageUserId = SESSION.user.id;
    

    $.widget('rk.visitPlanFinalCtrl', {
        options: {
            scheduleId: null
        },
        data:{},
        _create: function() {
            var me = this,
                elem = this.element,
                opt = this.options;
            //alert(opt.planId)
            /*if (opt.scheduleId && /^\d+$/.test(opt.scheduleId)) {
                $.getJSON('/json/crm_schedule/detail.action?scheduleId=' + opt.scheduleId, function(json) {
                    if (json.status == '0') {
                        if (json.data.schedule.accessible) {
                            me.showDetail(json.data);
                            me.addHandlers(json.data);
                        } else {
                            //elem.html('<div class="content_error"><p>您没有权限查看该日程</p></div>');
                            elem.html('<div class="helpful_box"><div class="helpful_content">您没有权限查看该日程</div></div>');
                        }
                    } else if (json.status == '300002') {
                        //elem.html('<div class="content_error"><p>该日程已被删除或退出</p></div>');
                        elem.html('<div class="helpful_box"><div class="helpful_content">该日程已被删除或退出</div></div>');
                    } else if (json.status == '300001') {
                        //elem.html('<div class="content_error"><p>您没有权限查看该日程</p></div>');
                        elem.html('<div class="helpful_box"><div class="helpful_content">您没有权限查看该日程</div></div>');
                    } else {
                        $.msg('加载日程数据出错啦：' + json.statusText);
                    }
                });
            }*/
            if(opt.planId){
                rk.post("/json/crm_visitPlan/detail-plan.action",{planId: opt.planId},function(json){
                    //moment(data.schedule.createdAt - 0).format("YYYY-MM-DD HH:mm")
                    if(json.status == 0) {
                        var data = json.body;
                        data.plan.createdDate = moment(data.plan.createdAt - 0).format("YYYY-MM-DD");
                        data.plan.createdTime = moment(data.plan.createdAt - 0).format("HH:mm");
                        data.plan.creater = data.users[data.plan.createdBy].name;
                        if(data.plan.frequency == 0) {
                            data.plan.duration = moment(data.plan.startDate - 0).format("YYYY-MM-DD") + "到" + moment(data.plan.endDate - 0).format("YYYY-MM-DD");
                        }else{
                            data.plan.duration = moment(data.plan.startDate - 0).format("YYYY-MM-DD") + "到" + moment(data.plan.finalFinishTime - 0).format("YYYY-MM-DD");
                        }
                        data.plan.name = "拜访："+data.plan.from.name;
                        data.frequencyRules = "";
                        if(data.plan.frequency == 2){
                            var week_days = data.plan.rules.split(',');
                            var week_days_json={
                                "1":"周一",
                                "2":"周二",
                                "3":"周三",
                                "4":"周四",
                                "5":"周五",
                                "6":"周六",
                                "7":"周日"
                            };
                            var endString = '';
                            for(var i=0;i<week_days.length;i++){
                                endString += week_days_json[week_days[i]];
                                if(i != week_days.length -1){
                                    endString += ',';
                                }
                            }
                            data.plan.frequencyRules = "计划将于每" + endString +"重复";
                        }
                        if(data.plan.frequency == 3){
                            data.plan.frequencyRules = "该计划将于每月 "+data.plan.rules +"重复";
                        }
                        var acceptMember = data.acceptMember;
                        var acceptMemberIcon = data.users[acceptMember[0]].icon;
                        if(!acceptMemberIcon){
                            acceptMemberIcon = rk.DEFAULT_USER_ICON;
                        }
                        data.plan.canModify = false;
                        if(data.plan.createdBy == SESSION.user.id && data.plan.finishStatus != 1){
                            data.plan.canModify = true;
                        }
                        var date = new Date();
                        data.plan.visitDate = date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate();
                        data.plan.members = '<a href="/final/user.action?uid='+acceptMember[0]+'" title="'+data.users[acceptMember[0]].name+'"><img ucard="uid='+acceptMember[0]+'" src="'+acceptMemberIcon+'" class="mCS_img_loaded"></a>';
                        var temp = require("oa/tmpl/schedule/visit/tpl_schedule_visit_plan_final.tpl");
                        var html = rk.templateText(temp,data);
                        elem.html(html);
                        me.data = data;
                    }else{
                       rk.noticeError("加载出错啦");
                    }
                });
            }
            me._listeners();
        },
        _listeners:function(){
            var me = this,
                elem = this.element,
                opt = this.options;
            elem.on("click","[act='modify_schedule']",function(){
                rk.dialog({
                    title: '编辑拜访计划',
                    //width:"930",
                    success: function (contentElem) {
                        var date = new Date();
                        var dateString = date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate();
                        require.async(["./visitPlanUpdateDialogCtrl"],function(){
                            contentElem.visitPlanUpdateDialogCtrl({
                                param:{
                                    planId:opt.planId,
                                    changeDate:dateString
                                }
                            });//初始化弹框内容的Ctrl
                        });
                    },
                    footer: ['cancel', {
                        act: 'ok', visible: true, text: '保存'
                    }]
                });
            });
            elem.on("click","[act='delete_schedule']",function(){
                var sname = $("#task_name_label").html();
                var confirmString = "";
                if(me.data.plan.frequency == 0){
                    confirmString = "拜访计划将删除";
                }
                if(me.data.plan.frequency == 2 || me.data.plan.frequency == 3){
                    confirmString = "拜访计划将自今天截止";
                }
                rk.confirm('删除拜访计划【' + sname + '】后，'+confirmString, function() {
                    $.post('/json/crm_visitPlan/delete-visit-plan-for-future.action', {
                        planId: opt.planId
                    }, null, 'json').done(function(json) {
                        if (json.status == 0) {
                            $.msg('删除成功', 1);
                        } else {
                            $.msg('删除失败');
                        }
                    });
                });
            });
        },
        _init:function(){

        }
    });

    return {
        init: function(rkContent, id) {
            rkContent.visitPlanFinalCtrl({
                planId: id
            });
        }
    }
});
